package com.itmyx.sys.controller;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.itmyx.base.controller.BaseController;
import com.itmyx.base.util.data.ParamData;
import com.itmyx.base.util.data.Result;
import com.itmyx.sys.service.UserService;
/**
 * 功能描述  控制器类
 * @类型名称 UserController
 * @版本 1.0
 * @创建者 
 * @创建时间 
 * @版权所有 @2016 ITMYX
 * @修改者 
 * @修改时间 
 * @修改描述
 */
@Controller
public class UserController extends BaseController{
	
    @Autowired
	private UserService userService;
	

    
	@RequestMapping(value="/getUserList",method=RequestMethod.GET)
	@RequiresPermissions("item:query")//执行queryItems需要"item:query"权限
	public @ResponseBody String getUserList(){
		Result result = null;
		ParamData param=this.getPageData();//获取请求数据
		logBefore(logger,"查询列表","getUserList",param);
		try {
			result = new Result(1, userService.getList(param), null, null);//执行业务逻辑
		} catch (Exception e) {
			result = new Result(0, null, "获取失败", null);
			e.printStackTrace();
		}
		logAfter(logger,result.toJson());
		return result.toJson();
	}
    
	@RequestMapping(value="/getUserById",method=RequestMethod.POST)
	public @ResponseBody String getUserById(){
		Result result = null;
		ParamData param=this.getPageData();//获取请求数据
		logBefore(logger,"根据主键获取","addUser",param);
		try {
			result = new Result(1,userService.getById(param), null, null);//执行业务逻辑
		} catch (Exception e) {
			e.printStackTrace();
		}
		logAfter(logger,result.toJson());
		return result.toJson();
	}
	
	@RequestMapping(value="/addUser",method=RequestMethod.POST)
	public @ResponseBody String addUser(){
		Result result = null;
		ParamData param=this.getPageData();//获取请求数据
		logBefore(logger,"添加","addUser",param);
		try {
			result = new Result(1, userService.add(param), null, null);//执行业务逻辑
		} catch (Exception e) {
			result = new Result(0, null, "添加失败", null);
			e.printStackTrace();
		}
		logAfter(logger,result.toJson());
		return result.toJson();
	}
    
	@RequestMapping(value="/updateUser",method=RequestMethod.POST)
	public @ResponseBody String updateUser(){
		Result result = null;
		ParamData param=this.getPageData();//获取请求数据
		logBefore(logger,"更新User","updateUser",param);
		try {
			if(userService.updateById(param)){
				result = new Result(1,null, "更新成功", null);
			}else{
				result = new Result(0,null, "更新失败", null);
			}
			
		} catch (Exception e) {
			result = new Result(0, null, "添加失败服务器异常", null);
			e.printStackTrace();
		}
		logAfter(logger,result.toJson());
		return result.toJson();
	}
	
	@RequestMapping(value="/deleteUserById",method=RequestMethod.POST)
	public @ResponseBody String deleteUserById(){
		Result result = null;
		ParamData param=this.getPageData();//获取请求数据
		logBefore(logger,"删除","deleteUserById",param);
		try {
			if(userService.deleteById(param)){
				result = new Result(1,null, "删除成功", null);//执行业务逻辑
			}else{
				result = new Result(0,null, "删除失败", null);//执行业务逻辑
			}
			
		} catch (Exception e) {
			result = new Result(0, null, "删除失败服务器异常", null);
			e.printStackTrace();
		}
		logAfter(logger,result.toJson());
		return result.toJson();
	}
}
